package com.gagakuai.domain.rules;

import com.gagakuai.domain.actions.Action;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @description: 规则基类
 * @author: houhong
 * @create: 2024-09-07 10:28
 **/
public abstract class AbstractRule implements Rule {

    protected List<Action> actions = new ArrayList<>();

    public final void addAction(Action... actions) {

        if (actions == null) return;
        this.actions.addAll(List.of(actions));
    }

    @Override
    public List<Action> getActions() {
        return this.actions;
    }


    /*
     *  用于多个rule 放置在规则组的时候，执行排序
     *  用于对规则对编排
     *
     */
    @Override
    public int compareTo(final Rule rule) {

        if (getPriority() < rule.getPriority()) {
            return -1;
        }
        if (getPriority() > rule.getPriority()) {
            return 1;
        }

        String otherName = rule.getName();
        return getName().compareTo(otherName);
    }

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AbstractRule basicRule = (AbstractRule) o;

        if (!Objects.equals(getPriority(), basicRule.getPriority())) return false;

        if (!Objects.equals(getName(), basicRule.getName())) return false;

        return true;
    }

    @Override
    public int hashCode() {

        int result = getName().hashCode();

        result = 31 * result + (getDescription() != null ? getDescription().hashCode() + 1 : 0);
        result = 31 * result + getPriority();

        return result;
    }
}
